package com.goodomen.garbage.score.constant;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
@Getter
public enum DateFormatEnum {

    /**
     * yyyyMMdd
     */
    NUMBER_DATE_STR("yyyyMMdd"),

    /**
     * yyyyMMddHHmmss
     */
    NUMBER_DATETIME_STR("yyyyMMddHHmmss"),
    /**
     * yyyy-MM-dd
     */
    DATE_STR("yyyy-MM-dd"),

    DATETIME_STR("yyyy-MM-dd HH:mm:ss"),

    DATETIME_NO_SECOND_STR("yyyy-MM-dd HH:mm"),

    TIME_STR("HH:mm:ss"),

    TIME_NO_SECOND_STR("HH:mm"),

    NUMBER_TIME_STR("HHmmss"),

    NUMBER_TIME_NO_YEAR_STR("MMddhhmmss"),

    SPECIAL_STR("yyyy-MM-dd"),
    ;
    private static final ZoneId ZONE = ZoneId.systemDefault();

    private DateTimeFormatter formatter;
    private String desc;

    DateFormatEnum(String desc) {
        this.desc = desc;
        this.formatter = DateTimeFormatter.ofPattern(desc);
    }

    /**
     * 格式化日期
     *
     * @param date 时间
     * @return 格式化后的时间字符串
     */
    public String format(LocalDateTime date) {
        if (date == null) {
            return null;
        }
        return getFormatter().format(date);
    }

    /**
     * 将指定的格式化日期，转化为LocalDateTime
     *
     * @param dateStr 格式化日期
     * @return LocalDateTime
     */
    public LocalDateTime parseDateTime(String dateStr) {
        if (dateStr == null) {
            return null;
        }
        return LocalDateTime.parse(dateStr, getFormatter());
    }

    /**
     * 将指定的格式化日期，LocalDate
     *
     * @param date 格式化日期
     * @return LocalDate
     */
    public LocalDate formatDateStr(String date) {
        if (date == null) {
            return null;
        }
        return LocalDate.parse(date, getFormatter());
    }

    /**
     * 时间字符串转成 LocalTime (HH:mm:ss)
     *
     * @param date 格式化日期
     * @return LocalTime
     */
    public LocalTime formatTimeStr(String date) {
        if (date == null) {
            return null;
        }
        return LocalTime.parse(date, getFormatter());
    }

    /**
     * 将指定的格式化日期，转化为Date(只要年月日)
     *
     * @param date 格式化日期
     * @return Date
     */
    public Date formatStrToDate(String date) {
        if (StringUtils.isNotBlank(date)) {
            try {
                LocalDate localDateTime = LocalDate.parse(date, getFormatter());
                Instant instant = localDateTime.atStartOfDay(ZONE).toInstant();
                return Date.from(instant);
            } catch (Exception e) {
                log.error("日期转换异常：", e);
            }
        }
        return null;
    }

    public static String getTimeStr(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            localDateTime = LocalDateTime.now();
        }
        String localDateTimeStr = localDateTime.toString();
        if (localDateTimeStr.length() == 16) {
            localDateTimeStr = localDateTimeStr + ":00";
        }
        if (StringUtils.isNotBlank(localDateTimeStr)) {
            if (localDateTimeStr.length() == 23) {
                localDateTimeStr = localDateTimeStr.substring(0, 19);
            }
            return localDateTimeStr.replace("T", " ");
        }
        return "";
    }

}
